package org.zjt.platform.druid.spring.boot.starter;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * @author <a href="juntao.zhang@qq.com">juntao.zhang</a>
 * @Description: Druid Dashboard 配置
 * @Package org.zjt.platform.druid.spring.boot.starter
 * @date 2018/3/15 16:35
 * @see
 */
@ConditionalOnClass(name = {"javax.servlet.Servlet","javax.servlet.Filter"})
@Configuration
@EnableConfigurationProperties({DruidProperties.class})
public class DruidWebAutoConfigure {

    public static final String PARAM_NAME_USERNAME = "loginUsername";
    public static final String PARAM_NAME_PASSWORD = "loginPassword";
    public static final String PARAM_NAME_ALLOW    = "allow";
    public static final String PARAM_NAME_DENY     = "deny";
    public static final String PARAM_NAME_RESET_ENABLE = "resetEnable";


    /**
     * 配置监控服务器
     * @return 返回监控注册的servlet对象
     */
    @Bean
    public ServletRegistrationBean viewServlet(DruidProperties druidProperties) {
        Assert.notNull(druidProperties,"请配置druid 信息");
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), druidProperties.getHomeUrl());
        if (!CollectionUtils.isEmpty(druidProperties.getAllowIp()))
            servletRegistrationBean.addInitParameter(PARAM_NAME_ALLOW, String.join(",",druidProperties.getAllowIp()));
        if (!CollectionUtils.isEmpty(druidProperties.getDenyIp()))
            servletRegistrationBean.addInitParameter(PARAM_NAME_DENY,  String.join(",",druidProperties.getDenyIp()));
        Assert.notNull(druidProperties,"请配置spring.datasource.monitor.loginUserName");
        servletRegistrationBean.addInitParameter(PARAM_NAME_USERNAME, druidProperties.getLoginUserName());
        Assert.notNull(druidProperties,"请配置spring.datasource.monitor.loginPassword");
        servletRegistrationBean.addInitParameter(PARAM_NAME_PASSWORD, druidProperties.getLoginPassword());
        servletRegistrationBean.addInitParameter(PARAM_NAME_RESET_ENABLE, "false");
        return servletRegistrationBean;
    }

    /**
     * 配置服务过滤器
     * @return 返回过滤器配置对象
     */
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
        return filterRegistrationBean;
    }
}
